

package Project.GUI;

import Project.bussiness.CaseHistory;
import Project.bussiness.Doctor;
import Project.bussiness.HealthCheckup;
import Project.bussiness.Patient;
import Project.bussiness.Treatment;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class ViewCase extends javax.swing.JInternalFrame {
    
    /** Creates new form ViewCase */
    int idPatient=0;
     Vector vCase=new Vector();
    public ViewCase() {
         try {
        
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        initComponents();
 
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comFilter = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtFilter = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListPatient = new javax.swing.JList();
        butOK = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHealthCheckup = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblCaseHistory = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblTreatment = new javax.swing.JTable();

        setClosable(true);
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 255)));
        jLabel1.setText("Filtered By");

        comFilter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Name" }));

        jLabel2.setText("Look For");

        ListPatient.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ListPatient.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ListPatientValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(ListPatient);

        butOK.setText("OK");
        butOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(jLabel1))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comFilter, 0, 169, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(jLabel2))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtFilter, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(68, Short.MAX_VALUE)
                .addComponent(butOK)
                .addGap(62, 62, 62))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(butOK)
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblHealthCheckup.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Date", "Doctor", "State of Health", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblHealthCheckup);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addContainerGap())
        );
        jTabbedPane1.addTab("Health Checkup", jPanel1);

        tblCaseHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Case History", "Addmission Date", "Dischange Date"
            }
        ));
        tblCaseHistory.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCaseHistoryMouseClicked(evt);
            }
        });

        jScrollPane3.setViewportView(tblCaseHistory);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addContainerGap())
        );
        jTabbedPane1.addTab("Case History", jPanel3);

        jButton1.setText("View");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font(".VnTimeH", 1, 24));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("View case history");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Treatment"));
        tblTreatment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Doctor", "Bed", "Reason", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(tblTreatment);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, 0, 441, Short.MAX_VALUE)
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(157, 157, 157)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(20, 20, 20)
                                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblCaseHistoryMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCaseHistoryMouseClicked
// TODO add your handling code here:
        int id=tblCaseHistory.getSelectedRow();
        CaseHistory ch=(CaseHistory)vCase.get(id);
        int caseId=ch.getCaseID();
        Vector vT=new Vector();
        try {
            vT=Treatment.getAllTreatmentByCaseID(caseId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
         DefaultTableModel tbl = new DefaultTableModel();   
         tbl.setColumnCount(4);
         Vector b2 = new Vector();
         b2.add("Doctor");
         b2.add("Bed");
         b2.add("Reason");
         b2.add("Date");
         tbl.setColumnIdentifiers(b2);    
        if(vT!=null)
        {
            for(int i=0;i<vT.size();i++)
            {
                Treatment tm=new Treatment();
                tm=(Treatment)vT.get(i);
                Vector a = new Vector();
                a.add(Doctor.getDoctorNameByID(tm.getDoctorID()));
                a.add(tm.getBedID());
                a.add(tm.getReason());
                a.add(tm.getDate());
                tbl.addRow(a);
            }
        }
         tblTreatment.setModel(tbl);
        

         
         
    }//GEN-LAST:event_tblCaseHistoryMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        // Set model for tblHealthCheckup table
         DefaultTableModel tbl = new DefaultTableModel();   
         tbl.setColumnCount(4);
         Vector b = new Vector();
         b.add("Date");
         b.add("Doctor");
         b.add("State of Health");
         b.add("Price");
         tbl.setColumnIdentifiers(b);           
        Vector vH=new Vector();
        try{   
             vH=HealthCheckup.getHealthCheckupByPatientId(idPatient);
        }catch(Exception e){
            e.printStackTrace();
        }
         //TableModel model = tblHealthCheckup.getModel();                  
         if (vH!=null){
             for(int i=0;i<vH.size();i++){
                HealthCheckup hc = (HealthCheckup) vH.get(i);
                Vector a = new Vector();
                a.add(hc.getDate());
                a.add(hc.getDoctorID());
                a.add(hc.getStateOfHealth());
                a.add(hc.getPrice());
                tbl.addRow(a);
             }
        }
        tblHealthCheckup.setModel(tbl);    
        vH=null;

        
        // Set model for tblCaseHistory table
         DefaultTableModel tbl2 = new DefaultTableModel();   
         tbl2.setColumnCount(3);
         Vector b2 = new Vector();
         b2.add("Case History");
         b2.add("Addmission Date");
         b2.add("Dischange Date");
         tbl2.setColumnIdentifiers(b2);     
         Vector vC=new Vector();
        try{   
             vC=CaseHistory.getCaseHistoryByPatientId(idPatient);
        }catch(Exception e){
            e.printStackTrace();
        }              
         if (vC!=null){
             for(int i=0;i<vC.size();i++){
                CaseHistory ch = (CaseHistory) vC.get(i);
                Vector a2 = new Vector();
                a2.add(ch.getCaseHistory());
                a2.add(ch.getAdmissionDate());
                if (ch.getDischargeDate().equalsIgnoreCase("1900-01-01 00:00:00.0"))
                    a2.add("");
                else  a2.add(ch.getDischargeDate());
                tbl2.addRow(a2);
             }
        }
        tblCaseHistory.setModel(tbl2);    
        vCase=vC;
        vC=null;        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void butOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butOKActionPerformed
        model.removeAllElements();
        v=null;
        String select = comFilter.getSelectedItem().toString();
        try{
            if (txtFilter.getText().length()==0){
                v=Patient.getAllPatient();
                System.out.println("NON");
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        if (txtFilter.getText().length()!=0){
            try{
                if (select=="ID"){
                    try{
                        v=Patient.getPatientByID(Integer.parseInt(txtFilter.getText()));
                    }catch(Exception e){
                        JOptionPane.showMessageDialog(this,"ID error");
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            try{
                if (select=="Name"){
                    v=Patient.getPatientByName(txtFilter.getText());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        if (v!=null){
            for(int i=0;i<v.size();i++){
                if(select=="Name"){
                    Patient dt = (Patient) v.get(i);
                    model.addElement(dt.getFirstName()+" "+dt.getLastName());
                } else{
                    Patient dt = (Patient) v.get(i);
                    model.addElement(dt.getPatientID());
                }
            }
        }
        ListPatient.setModel(model);
    }//GEN-LAST:event_butOKActionPerformed

    private void ListPatientValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ListPatientValueChanged
        int vt=ListPatient.getSelectedIndex();
        if(vt==-1){
            return;
        }
        Patient pa=new Patient();
        pa=(Patient)v.get(vt);
        idPatient=pa.getPatientID();
    }//GEN-LAST:event_ListPatientValueChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListPatient;
    private javax.swing.JButton butOK;
    private javax.swing.JComboBox comFilter;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tblCaseHistory;
    private javax.swing.JTable tblHealthCheckup;
    private javax.swing.JTable tblTreatment;
    private javax.swing.JTextField txtFilter;
    // End of variables declaration//GEN-END:variables
    Vector v = new Vector();    
    DefaultListModel model = new DefaultListModel();      

}
